package com.kbcss.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Addcmnt
 */
public class Addcmnt extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Addcmnt() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        RequestDispatcher rd;
    
        HttpSession ss=request.getSession();
         String disid=request.getParameter("disid");
         String cmnts=request.getParameter("comments");
         String id=(String)ss.getAttribute("id");
        int xx=0;
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
       Calendar calendar = new GregorianCalendar();
       String am_pm;
       int hour = calendar.get(Calendar.HOUR);
       int minute = calendar.get(Calendar.MINUTE);
       int second = calendar.get(Calendar.SECOND);
       if(calendar.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
      else
      am_pm = "PM";
       String date=sqlDate.toString();
       String time=hour + ":" + minute + ":" + second + " " + am_pm;
       try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/kbcssdb","root","mysql");
            PreparedStatement ps=con.prepareStatement("insert into comments(disid,userid,cmnt,cmntdate,cmnttime) values(?,?,?,?,?)");
            ps.setString(1, disid);
            ps.setString(2, id);
            ps.setString(3, cmnts);
            ps.setString(4, date);
            ps.setString(5, time);
            xx=ps.executeUpdate();
            out.println("xx="+xx);
            if(xx==1){
              rd=request.getRequestDispatcher("participatedisc.jsp?q="+disid);
			 rd.forward(request,response);
         }
        } catch(Exception e1)
		 {
                         out.println("xx="+xx);
			 out.println("error...."+e1);
                         out.close();
		 }
	}

}
